package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.model.IntentResponseModel;
import com.example.service.AudioService;

import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/api/audio")
@CrossOrigin(origins = "*")
public class AudioController {

	@Autowired
	private AudioService audioTranscriptionService;

	@PostMapping("/transcribe")
	public void uploadAudio(@RequestParam("audio") MultipartFile audioFile) {
		audioTranscriptionService.processAudio(audioFile);
	}

	@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	public Flux<ServerSentEvent<IntentResponseModel>> streamTranscription() {
		return audioTranscriptionService.getTranscriptionStream()
				.map(data -> ServerSentEvent.<IntentResponseModel> builder().data(data).build());
	}
}
